package com.lk2.core.config
{
	import com.lk2.core.util.LogFactory;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.logging.ILogger;
	

	/**
	 * The abstract class for application configuration management.
	 * This class shouldn't be used directly by application, even if Children doesn't implement or override functions
	 *  
	 * @author Luca Bianchi
	 * 
	 */
	public class AbsConfigObj implements IEventDispatcher, IConfig
	{
		private var _eventDispatcher:EventDispatcher;
		protected var configFile:String;
		protected var configXML:XML;
		protected var logger:ILogger;

		public function AbsConfigObj(configFileURL:String) 
		{
			//logger = LogFactory.getLogger(this);
			configFile = configFileURL;
			_eventDispatcher = new EventDispatcher();			
		}
		
		
		/**
		 * This is the most main function of the class, used to retreive parameters values
		 *  
		 * @param parameter the name of required parameter
		 * @return the value of desired parameter
		 * 
		 */
		public function getString(parameter:String):String{
			var p:XMLList = xmlParameters.param.(@name == parameter);
			if(p == null) throw new Error("Parameter Value not found"); 
			return p;	
		}
		
		
		/**
		 * This is used to retreive application parameters 
		 * @return an @see Array containig each parameter loaded
		 * 
		 */
		public function get arrayParameters():Array{
			var parameters:Array = new Array();
			for each(var item:XML in xmlParameters.param){
				parameters.push(""+item);
			}
			return parameters;
		}
		
		
		/**
		 * This function is used to retreive the whole XML parameter file 
		 * @return an XML file containing parameters
		 * 
		 */
		public function get xmlParameters():XML{
			return this.configXML;
		}
		
		public function hasEventListener(type:String):Boolean{
			return _eventDispatcher.hasEventListener(type);
		}
		
		public function willTrigger(type:String):Boolean{
			return _eventDispatcher.willTrigger(type);
		}
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void{
			_eventDispatcher.addEventListener(type,listener,useCapture,priority,useWeakReference);
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void{
			_eventDispatcher.removeEventListener(type,listener,useCapture);
		}
		
		public function dispatchEvent(event:Event):Boolean{
			return _eventDispatcher.dispatchEvent(event);
		}
		
		

	}
}